# Maintainer: Luis Martinez <luis.martinez@disroot.org>
# Contributor: Rafał Brzegowy <rafal.brzegowy@yahoo.com>

_realname=mfem
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=4.5.2
pkgrel=1
pkgdesc='Lightweight, general, scalable C++ library for finite element methods (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         #"${MINGW_PACKAGE_PREFIX}-adios2"
         "${MINGW_PACKAGE_PREFIX}-hypre"
         "${MINGW_PACKAGE_PREFIX}-netcdf"
         "${MINGW_PACKAGE_PREFIX}-metis"
         "${MINGW_PACKAGE_PREFIX}-mpfr"
         "${MINGW_PACKAGE_PREFIX}-msmpi"
         #$([[ ${MINGW_PACKAGE_PREFIX} == *-clang-* ]] || echo "${MINGW_PACKAGE_PREFIX}-mumps")
         "${MINGW_PACKAGE_PREFIX}-omp"
         "${MINGW_PACKAGE_PREFIX}-openblas"
         #"${MINGW_PACKAGE_PREFIX}-petsc"
         #"${MINGW_PACKAGE_PREFIX}-slepc"
         "${MINGW_PACKAGE_PREFIX}-suitesparse"
         "${MINGW_PACKAGE_PREFIX}-sundials")
         #"${MINGW_PACKAGE_PREFIX}-superlu")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
license=('spdx:BSD-3-Clause')
url='https://github.com/mfem/mfem'
source=("${_realname}-${pkgver}.tar.gz::$url/archive/v$pkgver.tar.gz")
sha256sums=('9431d72a2834078f25c58430767bf2fd62bf43a0feb003189a86847c68b8af4a')

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  #Static Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-static" && cd "${srcdir}/build-${MSYSTEM}-static"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=OFF \
      -DMFEM_USE_LAPACK=ON \
      -DMFEM_USE_MEMALLOC=ON \
      -DMFEM_USE_METIS=ON \
      -DMFEM_USE_MPFR=ON \
      -DMFEM_USE_MPI=ON \
      -DMFEM_USE_NETCDF=ON \
      -DMFEM_USE_OPENMP=ON \
      -DMFEM_USE_SUITESPARSE=ON \
      -DMFEM_USE_SUNDIALS=ON \
      -DMFEM_USE_ZLIB=ON \
      -DMFEM_ENABLE_TESTING=OFF \
      -DMFEM_ENABLE_EXAMPLES=OFF \
      -DMFEM_ENABLE_MINIAPPS=OFF \
      -DBLA_VENDOR=OpenBLAS \
      -DHDF5_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .

  #Shared Build
  mkdir -p "${srcdir}/build-${MSYSTEM}-shared" && cd "${srcdir}/build-${MSYSTEM}-shared"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -GNinja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DBUILD_SHARED_LIBS=ON \
      -DMFEM_USE_LAPACK=ON \
      -DMFEM_USE_MEMALLOC=ON \
      -DMFEM_USE_METIS=ON \
      -DMFEM_USE_MPFR=ON \
      -DMFEM_USE_MPI=ON \
      -DMFEM_USE_NETCDF=ON \
      -DMFEM_USE_OPENMP=ON \
      -DMFEM_USE_SUITESPARSE=ON \
      -DMFEM_USE_SUNDIALS=ON \
      -DMFEM_USE_ZLIB=ON \
      -DMFEM_ENABLE_TESTING=OFF \
      -DMFEM_ENABLE_EXAMPLES=OFF \
      -DMFEM_ENABLE_MINIAPPS=OFF \
      -DBLA_VENDOR=OpenBLAS \
      -DHDF5_DIR=${MINGW_PREFIX} \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  #Static Install
  cd "${srcdir}/build-${MSYSTEM}-static"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  #Shared Install
  cd "${srcdir}/build-${MSYSTEM}-shared"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/LICENSE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/README.md ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/README
  install -Dm644 ${srcdir}/${_realname}-${pkgver}/NOTICE ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/NOTICE

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in $(find "${pkgdir}${MINGW_PREFIX}"/lib/cmake -type f); do
    sed -e "s|${PREFIX_WIN}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
    sed -e "s|${MINGW_PREFIX}|\$\{_IMPORT_PREFIX\}|g" -i ${_f}
  done
}
